package com.ddm.controller.wxadmin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.ddm.constant.MessageConstant;
import com.ddm.constant.VXConstant;
import com.ddm.dto.page.PageQueryDTO;
import com.ddm.dto.vxDTO.QuestionDTO;
import com.ddm.entity.vxEntity.Question;
import com.ddm.exception.DataErrorException;
import com.ddm.result.PageResult;
import com.ddm.result.Result;
import com.ddm.service.vxService.QuetsionsService;
import com.ddm.utils.BeanCopyUtil;
import com.ddm.vo.vxVo.QuestionVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author horizon
 * @since 2025-01-17
 */
@RestController
@RequestMapping("/admin/question")
@Api("试题接口")
@Slf4j
public class QuetsionsController {


    @Autowired
    QuetsionsService quetsionsService;
    /**
     * 添加题目
     * @param questionDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加题目")
    public Result<QuestionVO> addQuestion(@RequestBody QuestionDTO questionDTO){
        log.info("添加题目:{}",questionDTO.getQuestionContent());
        if (questionDTO.getQuestionContent()==null || questionDTO.getStandardId()==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        LambdaQueryWrapper<Question> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Question::getQuestionContent,questionDTO.getQuestionContent());
        if (quetsionsService.getOne(wrapper) != null){
            throw  new DataErrorException(MessageConstant.ALREADY_EXISTS);
        }
        Question question = BeanCopyUtil.BeanCopy(questionDTO, Question.class);
        if (questionDTO.getQuestionType().toString().equals("SINGLE_CHOICE")){
            question.setQuestionScore(VXConstant.SINGLE_CHOICE_SCORE);
        }else {
            question.setQuestionScore(VXConstant.MULTIPLE_CHOICE_SCORE);
        }
        quetsionsService.saveQuestion(question);
        QuestionVO questionVO = BeanCopyUtil.BeanCopy(question, QuestionVO.class);
        return Result.success(questionVO);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除试题")
    public Result deleteQuestion(@PathVariable Long id ){
        log.info("根据id删除试题:{}",id);
        quetsionsService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 修改试题
     */
    @PutMapping
    @ApiOperation("修改试题")
    public Result updateQuestion(@RequestBody QuestionDTO questionDTO){
        log.info("修改试题:{}",questionDTO.getQuestionContent());
        if (questionDTO.getId() == null || questionDTO.getStandardId()==null ||questionDTO.getQuestionContent()==null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        Long id = questionDTO.getId();
        Question question = quetsionsService.getById(id);
        if (question ==null){
            return Result.error("试题不存在");
        }
        question = BeanCopyUtil.BeanCopy(questionDTO, Question.class);
        quetsionsService.updateById(question);
        return Result.success("修改成功");
    }

    /**
     * 根据id查询试题
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询试题")
    public Result<QuestionVO> selectById(@PathVariable Long id){
        log.info("根据id查询试题:{}",id);
        Question question = quetsionsService.getById(id);
        QuestionVO questionVO = BeanCopyUtil.BeanCopy(question, QuestionVO.class);
        return Result.success(questionVO);
    }

    @GetMapping
    @ApiOperation("分页查询试题")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getQuestionList(PageQueryDTO pageQueryDTO){
        log.info("分页查询试题");
        return Result.success(quetsionsService.getQuestionList(pageQueryDTO));
    }

    @GetMapping("/getSQList")
    @ApiOperation("分页查询单选题")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getSQList(PageQueryDTO pageQueryDTO){
        log.info("分页查询单选题");
        return Result.success(quetsionsService.getSQList(pageQueryDTO));
    }

    @GetMapping("/getMQList")
    @ApiOperation("分页查询多选题")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<PageResult> getMQList( PageQueryDTO pageQueryDTO){
        log.info("分页查询多选题");
        return Result.success(quetsionsService.getMQList(pageQueryDTO));
    }

    @GetMapping("/standard/{id}")
    @ApiOperation("根据标准id查询试题")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    public Result<List<QuestionVO>> getStandardQuestionList(@PathVariable Long id){
        log.info("根据标准id查询试题列表");
        if (id == null){
            throw new DataErrorException(MessageConstant.NULL_DATA);
        }
        return Result.success(quetsionsService.getStandardQuestionList(id));
    }
}

